package com.yztctech.a24_05_systembroadcast_network;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.Toast;
/**
 * 接收网络状态的广播接收器 并获取当前的网络类型
 * @author Bodhixu
 *
 */
public class NetWorkReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = manager.getActiveNetworkInfo();//获取网络信息;这里有一个权限需要申请
		if (info!=null) {
			if (info.isConnected()) {
				//获取当前网络类型
				String type = info.getTypeName();
				String subtypeName = info.getSubtypeName();
				Log.e("Tag", "网络已连接!" + type);
			}else {
				Log.e("Tag", "网络连接异常!");
			}
		}else {
			Log.e("Tag", "没有网络!");
		}
	}

}
